package com.lostandfound.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lostandfound.mapper.FindMapper;
import com.lostandfound.pojo.FindAny;
import com.lostandfound.pojo.PageBean;

import com.lostandfound.pojo.dto.PublishDto;
import com.lostandfound.pojo.vo.PublishVo;
import com.lostandfound.service.FindService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
@Slf4j
public class FindServiceImpl implements FindService {
    @Autowired
    private FindMapper findMapper;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @param begin
     * @param end
     * @return
     */
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, LocalDate begin, LocalDate end) {
        //设置分页参数
        PageHelper.startPage(page,pageSize);
        //执行分页
        Page<FindAny> p = (Page<FindAny>) findMapper.list(name,begin,end);
        //封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    /**
     * 根据id查询寻物表回显编辑
     * 传回数据做到统一
     * @param id
     * @return
     */
    @Override
    public PublishVo findById(Integer id) {
        FindAny byId = findMapper.findById(id);
        PublishVo publishVo = new PublishVo();
        BeanUtils.copyProperties(byId,publishVo);
        publishVo.setLocalDate(byId.getLostTime());
        publishVo.setLocalPlace(byId.getLostPlace());

        return publishVo;
    }

    /**
     * 删除寻物
     * @param id
     */
    @Override
    public void delete(Integer id) {
        findMapper.delete(id);
    }

    /**
     * 修改物品信息
     * @param publishDto
     */
    @Override
    public void update(PublishDto publishDto) {
        FindAny findAny = new FindAny();
        BeanUtils.copyProperties(publishDto,findAny);
        findAny.setReleaseTime(LocalDateTime.now());
        findAny.setLostTime(publishDto.getLocalDate());
        findAny.setLostPlace(publishDto.getLocalPlace());
        findMapper.update(findAny);
    }
}
